package com.weblog.weblog.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weblog.weblog.admin.model.vo.category.AddCategoryReqVO;
import com.weblog.weblog.admin.model.vo.category.DeleteCategoryReqVO;
import com.weblog.weblog.admin.model.vo.category.FindCategoryPageListReqVO;
import com.weblog.weblog.admin.model.vo.category.FindCategoryPageListRspVO;
import com.weblog.weblog.admin.service.AdminCategoryService;
import com.weblog.weblog.common.domain.dos.ArticleCategoryRelDO;
import com.weblog.weblog.common.domain.dos.CategoryDO;
import com.weblog.weblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.weblog.weblog.common.domain.mapper.CategoryMapper;
import com.weblog.weblog.common.enums.ResponseCodeEnum;
import com.weblog.weblog.common.exception.BizException;
import com.weblog.weblog.common.modul.vo.SelectRspVO;
import com.weblog.weblog.common.utils.PageResponse;
import com.weblog.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author gc
 * @class_name AdminCategoryServiceImpl
 * @date 2023年09月20日 13:41
 */
@Service
@Slf4j
public class AdminCategoryServiceImpl implements AdminCategoryService {

    private final CategoryMapper categoryMapper;

    private final ArticleCategoryRelMapper articleCategoryRelMapper;


    public AdminCategoryServiceImpl(CategoryMapper categoryMapper, ArticleCategoryRelMapper articleCategoryRelMapper) {
        this.categoryMapper = categoryMapper;
        this.articleCategoryRelMapper = articleCategoryRelMapper;
    }


    @Override
    public Response addCategory(AddCategoryReqVO addCategoryReqVO) {

        String categoryName = addCategoryReqVO.getName();

        //先判断改分类是否存在
        CategoryDO categoryDO = categoryMapper.selectByName(categoryName);
        if(Objects.nonNull(categoryDO)){
            log.warn("分类名称: {},此分类已存在", categoryName);
            throw new BizException(ResponseCodeEnum.CATEGORY_NAME_IS_EXISTED);
        }

        //构建DO类
        CategoryDO insertCategoryDO = CategoryDO.builder()
                .name(addCategoryReqVO.getName().trim())
                .build();

        //执行insert
        categoryMapper.insert(insertCategoryDO);

        return Response.success();
    }

    /**
     * 分类分页数据查询
     *
     */
    @Override
    public PageResponse findCategoryPageList(FindCategoryPageListReqVO findCategoryPageListReqVO) {
        //获取当前页
        Long current = findCategoryPageListReqVO.getCurrent();
        Long size = findCategoryPageListReqVO.getSize();
        String name = findCategoryPageListReqVO.getName();
        LocalDate startDate = findCategoryPageListReqVO.getStartDate();
        LocalDate endDate = findCategoryPageListReqVO.getEndDate();


        Page<CategoryDO> categoryDOPage = categoryMapper.selectPageList(current,size,name,startDate,endDate);

        List<CategoryDO> categoryDOS = categoryDOPage.getRecords();

        //DO转VO
        List<FindCategoryPageListRspVO> vos = null;
        if(!CollectionUtils.isEmpty(categoryDOS)){
            vos = categoryDOS.stream()
                    .map(categoryDO -> FindCategoryPageListRspVO.builder()
                            .id(categoryDO.getId())
                            .name(categoryDO.getName())
                            .createTime(categoryDO.getCreateTime())
                            .articlesTotal(categoryDO.getArticlesTotal())
                            .build())
                    .collect(Collectors.toList());
        }

        return PageResponse.success(categoryDOPage,vos);
    }

    /*
     * 删除
     * @author gc
     * @date 2023/10/19 16:50
     * @param deleteCategoryReqVO
     * @return com.weblog.weblog.common.utils.Response
     */
    @Override
    public Response deleteCategory(DeleteCategoryReqVO deleteCategoryReqVO) {
        //分类ID
        Long categoryId = deleteCategoryReqVO.getId();

        //检验改分类下是否已有文章
        ArticleCategoryRelDO articleCategoryRelDO = articleCategoryRelMapper.selectOneByCategoryId(categoryId);

        if(Objects.nonNull(articleCategoryRelDO)){
            log.warn("==> 此分类下已有文章，无法删除。categoryId: {}",categoryId);
            throw new BizException(ResponseCodeEnum.CATEGORY_CAN_NOT_DELETE);
        }

        categoryMapper.deleteById(categoryId);

        return Response.success();
    }

    @Override
    public Response findCategorySelectList() {
        //查询所以分类
        List<CategoryDO> categoryDOS = categoryMapper.selectList(null);

        //DO转VO
        List<SelectRspVO> selectRspVOS = null;
        //分类数据不为空
        if (!CollectionUtils.isEmpty(categoryDOS)){
            //将分类ID作为Value值，将分类名称作为label展示
            selectRspVOS = categoryDOS.stream()
                    .map(categoryDO -> SelectRspVO.builder()
                            .label(categoryDO.getName())
                            .value(categoryDO.getId())
                            .build())
                    .collect(Collectors.toList());
        }

        return Response.success(selectRspVOS);
    }
}
